import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';

class KqReasonDialog extends Dialog {
  //标题
  final String title;

  ///每一项的
  final String Function(int index)? renderTitleCallback;

  ///每一项的ID
  final String Function(int index)? renderIdCallback;

  //选中的
  final String? chosenDataId;

  /// 个数
  final int? totalCount;

  //点击了哪一项
  final Function(int index)? callBack;

  const KqReasonDialog(
      {super.key,
      required this.title,
      required this.renderTitleCallback,
      required this.totalCount,
      this.renderIdCallback,
      this.chosenDataId,
      this.callBack});

  Widget _renderItemWidget(int index) {
    String text = renderTitleCallback!(index);
    String? itemId;
    if (renderIdCallback != null) {
      itemId = renderIdCallback!(index);
    }
    String? chosenId = chosenDataId;
    Color bgColor = KqThemeColors.bgF5;
    Color txtColor =
        KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
            KqThemeColors.text26;
    if (itemId != null && chosenId != null && itemId == chosenId) {
      //存在选中
      bgColor = KqThemeColors.bgBlue;
      txtColor = KqThemeColors.textWhite;
    }
    return InkWell(
      onTap: () {
        if (callBack != null) {
          callBack!(index);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2.r),
          color: bgColor,
        ),
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 12.r),
        child: Text(
          text,
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          textAlign: TextAlign.center,
          style: TextStyle(color: txtColor, fontSize: 12.sp),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    int total = totalCount!;
    return Center(
      child: Container(
        width: 320.r,
        padding: EdgeInsets.only(bottom: 16.r),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8.r),
          color: KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
              KqThemeColors.bgWhite,
        ),
        constraints: BoxConstraints(
            maxHeight: total <= 0
                ? 48.r + 58.r + 16.r
                : (total / 3.0).ceil() >= 6
                    ? 48.r * 6 + 58.r + 16.r
                    : 48.r * (total / 3.0).ceil() + 58.r + 16.r),
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(
                  top: 24.r, bottom: 16.r, left: 16.r, right: 16.r),
              child: Text(
                title,
                maxLines: 1,
                style: TextStyle(
                    fontSize: 16.sp,
                    color: KqThemeColors.text26,
                    fontWeight: FontWeight.w600),
              ),
            ),
            Expanded(
              child: GridView.builder(
                shrinkWrap: true,
                padding: EdgeInsets.only(left: 11.r, right: 11.r),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 8.r,
                  mainAxisSpacing: 8.r,
                  childAspectRatio: 47 / 20.0,
                ),
                itemCount: total,
                itemBuilder: (BuildContext context, int index) {
                  return _renderItemWidget(index);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
